package kr.couponplay.android.activity;

import java.util.ArrayList;

import kr.couponplay.android.AppContext;
import kr.couponplay.android.Category;
import kr.couponplay.android.domain.ButtonView.MenuType;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;
import android.widget.Toast;

public class CategoryActivity extends BaseActivity implements OnChildClickListener {
	CategoryExpandableAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_category);

		ExpandableListView listView = (ExpandableListView) findViewById(R.id.category_expandableListView);
		listView.setGroupIndicator(null);

		adapter = new CategoryExpandableAdapter(this, AppContext.categoryGroups, AppContext.categoryChildren);
		listView.setAdapter(adapter);
		
		listView.setOnChildClickListener(new OnChildClickListener() {
			@Override
			public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
				Category category = adapter.getChild(groupPosition, childPosition).get(0);
				AppContext.clearSearch();
    			
    			boolean isMainMenu = (category.getParentId() == 0);
    			AppContext.mainMenuId = (isMainMenu)? category.getId() : category.getParentId();
    			AppContext.subMenuId = (isMainMenu)? 0 : category.getId();
    			finish();
    			Intent intent = new Intent(mContext, MainActivity.class);
    			String title = "카테고리";
    			title += " > " + adapter.getGroup(groupPosition).getName();
    			if (!isMainMenu) {
    				title += " > " + adapter.getChild(groupPosition, childPosition).get(0).getName();
    			}
    			intent.putExtra("title", title);
    			intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    			intent.putExtra("menuType", MenuType.CATEGORY.name());
    			startActivity(intent);
				return false;
			}
		});
	}
	
	@Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
		Toast toast = Toast.makeText(this, "child clicked : " + childPosition, Toast.LENGTH_SHORT); 
		toast.show();
		return true;
	}	

	public class CategoryExpandableAdapter extends BaseExpandableListAdapter {
		private ArrayList<Category> groups;
		private ArrayList<ArrayList<ArrayList<Category>>> children;
		private Context context;

		public CategoryExpandableAdapter(Context context, ArrayList<Category> groups, ArrayList<ArrayList<ArrayList<Category>>> children) {
			this.context = context;
			this.groups = groups;
			this.children = children;
		}

		@Override
		public boolean areAllItemsEnabled() {
			return true;
		}

		@Override
		public ArrayList<Category> getChild(int groupPosition, int childPosition) {
			return children.get(groupPosition).get(childPosition);
		}

		@Override
		public long getChildId(int groupPosition, int childPosition) {
			return childPosition;
		}

		@Override
		public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
			Category child = (Category) ((ArrayList<Category>) getChild(groupPosition, childPosition)).get(0);

			if (convertView == null) {
				LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = infalInflater.inflate(R.layout.category_child, null);
			}

			TextView childtxt = (TextView) convertView.findViewById(R.id.category_child_TextView);
			childtxt.setText(child.getName());
			return convertView;
		}

		@Override
		public int getChildrenCount(int groupPosition) {
			return children.get(groupPosition).size();
		}

		@Override
		public Category getGroup(int groupPosition) {
			return groups.get(groupPosition);
		}

		@Override
		public int getGroupCount() {
			return groups.size();
		}

		@Override
		public long getGroupId(int groupPosition) {
			return groupPosition;
		}

		@Override
		public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
			Category group = (Category) getGroup(groupPosition);

			if (convertView == null) {
				LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				convertView = infalInflater.inflate(R.layout.category_group, null);
			}

			TextView grouptxt = (TextView) convertView.findViewById(R.id.catetory_group_TextView);
			grouptxt.setText(group.getName());
			return convertView;
		}

		@Override
		public boolean hasStableIds() {
			return true;
		}

		@Override
		public boolean isChildSelectable(int arg0, int arg1) {
			return true;
		}
	}
}
